package com.zsh.servlet;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class HelloServlet implements Servlet {
    public HelloServlet(){
        System.out.println("1 gou zoaqi");
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

        System.out.println("2 初始化");
//        1、可以获取 Servlet 程序的别名 servlet-name 的值
        System.out.println("bieming :" + servletConfig.getServletName());
//        2、获取初始化参数 init-param
        System.out.println("init : " + servletConfig.getInitParameter("url"));
//        3、获取 ServletContext 对象
    }

    @Override
    public ServletConfig getServletConfig() {

        return null;
    }

    /**
     * service是专门用来处理请求和响应的
     * @param servletRequest
     * @param servletResponse
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("3 Hello, Servlet 呗访问了");
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String method = httpServletRequest.getMethod();

        if("GET".equals(method)){
            System.out.println("GET");
        }else if("POST".equals(method)){
            System.out.println("POST");
        }
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("4 destroy");
    }
}
